package ac.ui.controls;

import ac.engin.treasury.treasuryData;
import ac.settings.dataModel;
import ac.ui.custemItmes;
import ac.ui.utils;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author ismail
 */
public class PCLTree extends JTree implements dataModel {

    private String addtype;
    ac.uitls.propertiesOperation op = new ac.uitls.propertiesOperation();
    ac.uitls.xml x = new ac.uitls.xml();

    private boolean locked;

    public boolean isLocked() {
        return locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public PCLTree() {

        if (!isLocked()) {
            ac.ui.controls.PCLpopupMenu treemenu = new ac.ui.controls.PCLpopupMenu();

            javax.swing.JMenuItem addToTree = new javax.swing.JMenuItem();

            addToTree.setText(op.getWord("label.add"));

            treemenu.add(addToTree);

            javax.swing.JMenuItem editToTree = new javax.swing.JMenuItem();

            editToTree.setText(op.getWord("label.edit"));

            treemenu.add(editToTree);
            javax.swing.JMenuItem deleteToTree = new javax.swing.JMenuItem();
            deleteToTree.setText(op.getWord("label.delete"));
            treemenu.add(deleteToTree);

            javax.swing.JMenuItem reToTree = new javax.swing.JMenuItem();
            reToTree.setText(op.getWord("label.refresh"));
            treemenu.add(reToTree);

            addToTree.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    addToTreeActionPerformed(evt);
                }
            });

            editToTree.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    editToTreeActionPerformed(evt);
                }
            });

            deleteToTree.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    deleteToTreeActionPerformed(evt);
                }
            });
            reToTree.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    reToTreeToTreeActionPerformed(evt);
                }
            });

            addKeyListener(new java.awt.event.KeyAdapter() {
                public void keyPressed(java.awt.event.KeyEvent evt) {

                    if (evt.getKeyCode() == KeyEvent.VK_F5) {
                        reToTreeToTreeActionPerformed(null);
                    } else if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
                        deleteToTreeActionPerformed(null);
                    } else if (evt.getKeyCode() == KeyEvent.VK_F6) {
                        addToTreeActionPerformed(null);
                    } else if (evt.getKeyCode() == KeyEvent.VK_F7) {
                        editToTreeActionPerformed(null);
                    }
                }

            });

            treemenu.add(addToTree);

            setComponentPopupMenu(treemenu);
        }
    }

    void getFrame(String Status
    ) {
        Map m;
        m = x.getTreeAdds(getAddtype());

        String type = "" + m.get("type");

        String title = "" + m.get("title");

        ac.GUI.controls.treeaddsf p = new ac.GUI.controls.treeaddsf(Status, this.getValue());
        p.Type = type;
        p.parent = this;
        p.setTitle(op.getWord(title));

        p.setVisible(true);

    }

    public void addToTreeActionPerformed(java.awt.event.ActionEvent evt) {

        getFrame("add");

    }

    public void editToTreeActionPerformed(java.awt.event.ActionEvent evt) {

        getFrame("edit");

    }

    String tabel = tableAddstree;

    public void deleteToTreeActionPerformed(java.awt.event.ActionEvent evt) {

        ac.ui.FrameOpertion f = new ac.ui.FrameOpertion();
        f.TabelString = tabel;
        f.IndexID = Integer.parseInt(this.getValue() );

        f.addDeletTemp(true);
        updateData();
    }

    public void reToTreeToTreeActionPerformed(java.awt.event.ActionEvent evt) {
        updateData();
    }

    //ac.lib.database.operations db =new ac.lib.database.operations();
    public String getAddtype() {
        return addtype;
    }

    public void setAddtype(String addtype) {
        this.addtype = addtype;
    }
    ac.lib.database.operations db = new ac.lib.database.operations();

    public void updateData() {
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new Item("0", op.getWord("label.all")));

        db.setOpenCon();
        ResultSet rs = db.getDataWithOutCon(tableAddstree, "`id`,`title`", "type='" + getAddtype() + "' and pid='0'  and rowstatus='0'");

        DefaultTreeModel t = new DefaultTreeModel(rootNode);

        try {
            while (rs.next()) {

                DefaultMutableTreeNode dataNode = new DefaultMutableTreeNode(new Item(rs.getString("id"), rs.getString("title")));
                rootNode.add(dataNode);

                getch(dataNode, rs.getString("id"));

            }
            setModel(t);

        } catch (SQLException ex) {
            Logger.getLogger(PCLcomboboxAdd.class.getName()).log(Level.SEVERE, null, ex);
        }

        db.setCloseCon();

    }

    public void getch(DefaultMutableTreeNode dataNode, String id) {

        ResultSet rsc = db.getDataWithOutCon(tableAddstree, "`id`,`title`", "type='" + getAddtype() + "' and pid='" + id + "' and rowstatus='0'");
        try {
            while (rsc.next()) {
                DefaultMutableTreeNode dataNodec = new DefaultMutableTreeNode(new Item(rsc.getString("id"), rsc.getString("title")));
                dataNode.add(dataNodec);
                getch(dataNodec, rsc.getString("id"));

            }
        } catch (SQLException ex) {
            Logger.getLogger(PCLTree.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void selectBYValue(String value) {

        for (int i = 0; i < this.getRowCount(); i++) {
            if (value != null) {

                TreePath tnode;
                tnode = this.getPathForRow(i);

                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tnode.getLastPathComponent();

                Object nodeInfo = node.getUserObject();

                ac.ui.controls.Item itemCate = (ac.ui.controls.Item) nodeInfo;

                if (itemCate.getValue().equals(value)) {
                    this.setSelectionRow(i);
                }
            }

        }

    }

    public String getValue() {

        String returnData = "";
        try {

            DefaultMutableTreeNode node = (DefaultMutableTreeNode) this.getLastSelectedPathComponent();
            Object nodeInfo = node.getUserObject();
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) nodeInfo;
            returnData = itemCate.getValue();
        } catch (Exception ex) {
        }

        return returnData;
    }

    public String getText() {

        String returnData = "";
        try {

            DefaultMutableTreeNode node = (DefaultMutableTreeNode) this.getLastSelectedPathComponent();
            Object nodeInfo = node.getUserObject();
            ac.ui.controls.Item itemCate = (ac.ui.controls.Item) nodeInfo;
            returnData = itemCate.getText();
        } catch (Exception ex) {
        }

        return returnData;
    }

}
